package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 付诚
 * @Date: 2024/5/21 15:06
 * @Description:
 */
@Service
public class WxHomeServiceImpl_fc implements WxHomeService_fc {
    @Autowired
    private MarketAdMapper marketAdMapper;
    @Autowired
    private MarketBrandMapper marketBrandMapper;
    @Autowired
    private MarketCategoryMapper marketCategoryMapper;
    @Autowired
    private MarketCouponMapper marketCouponMapper;
    @Autowired
    private MarketCouponUserMapper marketCouponUserMapper;
    @Autowired
    private MarketGoodsMapper marketGoodsMapper;
    @Autowired
    private MarketTopicMapper marketTopicMapper;


    @Override
    public Map<String, Object> index() {
        Map<String, Object> data = new HashMap<>();

        List<MarketAd> banner = marketAdMapper.selectByExample(new MarketAdExample());
        List<MarketBrand> brandList = marketBrandMapper.selectByExample(new MarketBrandExample());
        List<MarketCategory> channel = marketCategoryMapper.selectByExample(new MarketCategoryExample());
        List<MarketCoupon> couponList = marketCouponMapper.selectByExample(new MarketCouponExample());

        List<Object> floorGoodsList = new ArrayList<>();
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria1 = marketCategoryExample.createCriteria();
        criteria1.andLogicalDeleted(true);
        criteria1.andLevelEqualTo("L1");
        // 查询一级分类
        List<MarketCategory> marketCategories1 = marketCategoryMapper.selectByExample(marketCategoryExample);
        for (MarketCategory marketCategory : marketCategories1) {

            MarketCategoryExample marketCategoryExample1 = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria8 = marketCategoryExample1.createCriteria();
            criteria8.andLogicalDeleted(true);
            criteria8.andPidEqualTo(marketCategory.getId());
            // 查询二级分类
            List<MarketCategory> marketCategories2 = marketCategoryMapper.selectByExample(marketCategoryExample1);
            List<Integer> Ids = new ArrayList<>();
            for (MarketCategory category : marketCategories2) {
                Ids.add(category.getId());
            }
            Ids.add(marketCategory.getId());

            MarketGoodsExample marketGoodsExample1 = new MarketGoodsExample();
            MarketGoodsExample.Criteria criteria = marketGoodsExample1.createCriteria();
            criteria.andLogicalDeleted(true);
            criteria.andCategoryIdIn(Ids);
            List<MarketGoods> marketGoods1 = marketGoodsMapper.selectByExample(marketGoodsExample1);
            HashMap<String, Object> marketGoodsMap = new HashMap<>();
            marketGoodsMap.put("name", marketCategory.getName());
            marketGoodsMap.put("goodsList", marketGoods1);
            marketGoodsMap.put("id", marketCategory.getId());
            floorGoodsList.add(marketGoodsMap);
        }


        List<MarketGoods> hotGoodsList = marketGoodsMapper.selectByExample(new MarketGoodsExample());
        List<MarketGoods> newGoodsList = marketGoodsMapper.selectByExample(new MarketGoodsExample());

        List<MarketTopic> topicList = marketTopicMapper.selectByExample(new MarketTopicExample());


        data.put("banner", banner);
        data.put("brandList", brandList);
        data.put("channel", channel);
        data.put("couponList", couponList);
        data.put("floorGoodsList", floorGoodsList);
        data.put("hotGoodsList", hotGoodsList);
        data.put("newGoodsList", newGoodsList);
        data.put("topicList", topicList);

        return data;
    }
}
